<template>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
            <el-form-item label="从站编号：" prop="slaveId">
                <el-input-number v-model="form.slaveId"></el-input-number>
            </el-form-item>
            <el-form-item label="功能码：" prop="functionCode">
                <el-input-number v-model="form.functionCode" placeholder="请输入功能码"></el-input-number>
            </el-form-item>
            <el-form-item label="偏移量：" prop="offset">
                <el-input-number v-model="form.offset" placeholder="请输入偏移量"></el-input-number>
            </el-form-item>
            <el-form-item label="属性类型：" prop="attributeType">
                <el-select v-model="form.attributeType" style="height: auto;">
                    <el-option v-for="dict in attributeTypeOptions"
                               :key="dict.dictValue"
                               :label="dict.dictLabel"
                               :value="dict.dictValue">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addParseAttribute, updateParseAttribute} from '/src/api/driver/parseModbus'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            attributeTypeOptions: Array,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    slaveId: [
                        {required: true, message: "从站编号不能为空", trigger: "blur"}
                    ],
                    functionCode: [
                        {required: true, message: "功能码不能为空", trigger: "blur"}
                    ],
                    offset: [
                        {required: true, message: "偏移量不能为空", trigger: "blur"}
                    ],
                    attributeType: [
                        {required: true, message: "类型不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.modbusId != undefined) {
                            updateParseAttribute(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addParseAttribute(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            }
        }
    }
</script>

<style scoped>

</style>
